package com.beemans.common.utils.permissions

import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import com.beemans.common.R
import com.beemans.common.ext.toString2
import com.beemans.common.utils.PermissionHelper
import com.hjq.permissions.Permission

/**
 * @author ti
 * @date 2022/6/28.
 */
object PermissionNameConvert {

    /**
     * 获取权限提示语
     */
    @SuppressLint("ResourceType")
    fun getPermissionHint(context: Context, vararg permissions: String): String {
        if (permissions.isEmpty()) {
            return R.string.common_permission_manual_fail_hint.toString2(context)
        }
        val hints = getPermissionString(context, *permissions)
        if (hints.isNotEmpty()) {
            return R.string.common_permission_manual_assign_fail_hint.toString2(context, hints)
        }
        return R.string.common_permission_manual_fail_hint.toString2(context)
    }

    /**
     * 获取权限名称
     */
    fun getPermissionString(context: Context, vararg permissions: String): String {
        return listToString(
            permissionsToStrings(
                context,
                PermissionUtils.asArrayList(*permissions)
            )
        )
    }

    /**
     * String 列表拼接成一个字符串
     */
    private fun listToString(hints: List<String>): String {
        if (hints.isEmpty()) {
            return ""
        }
        val builder = StringBuilder()
        for (text in hints) {
            if (builder.isEmpty()) {
                builder.append(text)
            } else {
                builder.append("、")
                    .append(text)
            }
        }
        return builder.toString()
    }

    /**
     * 将权限列表转换成对应名称列表
     */
    @SuppressLint("ResourceType")
    private fun permissionsToStrings(context: Context, permissions: List<String>): List<String> {
        val permissionNames = ArrayList<String>()
        for (permission in permissions) {
            if (PermissionHelper.isGranted(permission)) {
                continue
            }
            when (permission) {
                Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE -> {
                    val hint = R.string.common_permission_storage.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.CAMERA -> {
                    val hint = R.string.common_permission_camera.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.RECORD_AUDIO -> {
                    val hint = R.string.common_permission_microphone.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_COARSE_LOCATION, Permission.ACCESS_BACKGROUND_LOCATION -> {
                    val hint = if (!permissions.contains(Permission.ACCESS_FINE_LOCATION) &&
                        !permissions.contains(Permission.ACCESS_COARSE_LOCATION)
                    ) {
                        R.string.common_permission_location_background.toString2(context)
                    } else {
                        R.string.common_permission_location.toString2(context)
                    }
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.BLUETOOTH_SCAN, Permission.BLUETOOTH_CONNECT, Permission.BLUETOOTH_ADVERTISE -> {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                        val hint = R.string.common_permission_bluetooth.toString2(context)
                        if (!permissionNames.contains(hint)) {
                            permissionNames.add(hint)
                        }
                    }
                }
                Permission.READ_PHONE_STATE, Permission.CALL_PHONE, Permission.ADD_VOICEMAIL, Permission.USE_SIP, Permission.READ_PHONE_NUMBERS, Permission.ANSWER_PHONE_CALLS -> {
                    val hint = R.string.common_permission_phone.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.GET_ACCOUNTS, Permission.READ_CONTACTS, Permission.WRITE_CONTACTS -> {
                    val hint = R.string.common_permission_contacts.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.READ_CALENDAR, Permission.WRITE_CALENDAR -> {
                    val hint = R.string.common_permission_calendar.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.READ_CALL_LOG, Permission.WRITE_CALL_LOG, Permission.PROCESS_OUTGOING_CALLS -> {
                    val stringId =
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) R.string.common_permission_call_log else R.string.common_permission_phone
                    val hint = stringId.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.BODY_SENSORS -> {
                    val hint = R.string.common_permission_sensors.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.ACTIVITY_RECOGNITION -> {
                    val hint = R.string.common_permission_activity_recognition.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.ACCESS_MEDIA_LOCATION -> {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                        val hint = R.string.common_permission_media_location.toString2(context)
                        if (!permissionNames.contains(hint)) {
                            permissionNames.add(hint)
                        }
                    }
                }
                Permission.SEND_SMS, Permission.RECEIVE_SMS, Permission.READ_SMS, Permission.RECEIVE_WAP_PUSH, Permission.RECEIVE_MMS -> {
                    val hint = R.string.common_permission_sms.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.MANAGE_EXTERNAL_STORAGE -> {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                        val hint = R.string.common_permission_manage_storage.toString2(context)
                        if (!permissionNames.contains(hint)) {
                            permissionNames.add(hint)
                        }
                    }
                }
                Permission.REQUEST_INSTALL_PACKAGES -> {
                    val hint = R.string.common_permission_install.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.SYSTEM_ALERT_WINDOW -> {
                    val hint = R.string.common_permission_window.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.WRITE_SETTINGS -> {
                    val hint = R.string.common_permission_setting.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.NOTIFICATION_SERVICE -> {
                    val hint = R.string.common_permission_notification.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.BIND_NOTIFICATION_LISTENER_SERVICE -> {
                    val hint = R.string.common_permission_notification_listener.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.PACKAGE_USAGE_STATS -> {
                    val hint = R.string.common_permission_task.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.SCHEDULE_EXACT_ALARM -> {
                    val hint = R.string.common_permission_alarm.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.ACCESS_NOTIFICATION_POLICY -> {
                    val hint = R.string.common_permission_not_disturb.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS -> {
                    val hint = R.string.common_permission_ignore_battery.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                Permission.BIND_VPN_SERVICE -> {
                    val hint = R.string.common_permission_vpn.toString2(context)
                    if (!permissionNames.contains(hint)) {
                        permissionNames.add(hint)
                    }
                }
                else -> {}
            }
        }
        return permissionNames
    }
}